/*
 * Copyright (c) 2011, Eriq Muhammad Adams J.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Institute Technology of Sepuluh Nopember nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ERIQ MUHAMMAD ADAMS J. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.pathmachine.ui;

import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.input.RawInputListener;
import com.jme3.input.event.JoyAxisEvent;
import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.math.FastMath;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture;

/**
 * Software Mouse Cursor Implementation
 * 
 * @author <a href="mailto:eriq.adams@gmail.com">Eriq Muhammad Adams J.</a>
 */
public class Cursor implements RawInputListener {

    private Picture cursor;
    private int cursorHeight;
    private int cursorWidth;
    private int screenHeight;
    private int screenWidth;
    private float x = 0, y = 0;

    public Cursor(String fileName, int cursorWidth, int cursorHeight, int screenWidth, int screenHeight, AssetManager assetManager, InputManager inputManager) {
        Texture tex = assetManager.loadTexture(fileName);
        cursor = new Picture("cursor");
        cursor.setTexture(assetManager, (Texture2D) tex, true);
        cursor.setWidth(cursorWidth);
        cursor.setHeight(cursorHeight);
        this.cursorHeight = cursorHeight;
        this.cursorWidth = cursorWidth;
        this.screenHeight = screenHeight;
        this.screenWidth = screenWidth;
        this.x = inputManager.getCursorPosition().x;
        this.y = inputManager.getCursorPosition().y;
        inputManager.addRawInputListener(this);

    }

    public Cursor(int screenWidth, int screenHeight, AssetManager assetManager, InputManager inputManager) {
        this("Interface/Cursor2.png", 32, 32, screenWidth, screenHeight, assetManager, inputManager);
    }

    public void beginInput() {
    }

    public void endInput() {
    }

    public void onJoyAxisEvent(JoyAxisEvent evt) {
    }

    public void onJoyButtonEvent(JoyButtonEvent evt) {
    }

    public void onMouseMotionEvent(MouseMotionEvent evt) {
        x += evt.getDX();
        y += evt.getDY();
        // Prevent mouse from leaving screen
        x = FastMath.clamp(x, 0, screenWidth);
        y = FastMath.clamp(y, 0, screenHeight);

        // adjust for hotspot
        cursor.setPosition(x, y - getCursorHeight());
    }

    public void onMouseButtonEvent(MouseButtonEvent evt) {
    }

    public void onKeyEvent(KeyInputEvent evt) {
    }

    /**
     * @return the cursor
     */
    public Picture getCursor() {
        return cursor;
    }

    /**
     * @return the cursorHeight
     */
    public int getCursorHeight() {
        return cursorHeight;
    }

    /**
     * @return the cursorWidth
     */
    public int getCursorWidth() {
        return cursorWidth;
    }
}
